<?php
namespace V1\Model;
use Think\Model;
/**
 * 
 */
class UserTaskModel extends Model {
 
   /* 模型自动完成 */
    protected $_auto = array(
            array('period', 'getPeriod', self::MODEL_BOTH, 'function'),
            array('create_date', 'getCreateDate', self::MODEL_BOTH, 'callback'),
            array('create_time', NOW_TIME),
            array('status', '1'),
        );
    
    public function getCreateDate(){
            return date('Y-m-d',NOW_TIME);
    }
    
    public function add_sign($task_id,$user_id,$task_type){

        $create_date = $this->getCreateDate();
        
        $data =['task_id'=>$task_id,'user_id'=>$user_id,'task_type'=>$task_type,'create_date'=>$create_date];
        
        $user_task_info = $this->where($data)->find();
        if(!empty($user_task_info)){
            return -3;
        }
         if($this->create($data)){
                   $tid = $this->add();
                   return $tid ? $tid : -2; //0-未知错误，大于0-添加成功
        } else {
                   return $this->getError(); //错误详情见自动验证注释
        }
    }
    public function add_register($task_id,$user_id,$task_type){

        $data =['task_id'=>$task_id,'user_id'=>$user_id,'task_type'=>$task_type];
        $user_task_info = $this->where($data)->find();
        if(!empty($user_task_info)){
            return -3;
        }
        if($this->create($data)){
                   $tid = $this->add();
                   return $tid ? $tid : -2; //0-未知错误，大于0-添加成功
        } else {
                   return $this->getError(); //错误详情见自动验证注释
        }
    }
      public function add_join_gul($task_id,$user_id,$task_type){

        $data =['task_id'=>$task_id,'user_id'=>$user_id,'task_type'=>$task_type];
       
        $user_task_info = $this->where($data)->find();
        if(!empty($user_task_info)){
            return -3;
        }
        
        $game_count = M('UserGame')->where(['game_type'=>'gul','user_id'=>$user_id,'status'=>1])->count();
        if(empty($game_count)){
            return -4;
        }
        if($this->create($data)){
                   $tid = $this->add();
                   return $tid ? $tid : -2; //0-未知错误，大于0-添加成功
        } else {
                   return $this->getError(); //错误详情见自动验证注释
        }
    }
      public function add_join_extremun($task_id,$user_id,$task_type){

        $data =['task_id'=>$task_id,'user_id'=>$user_id,'task_type'=>$task_type];
       
        $user_task_info = $this->where($data)->find();
        if(!empty($user_task_info)){
            return -3;
        }
        
        $game_count = M('UserGame')->where(['game_type'=>'extremun','user_id'=>$user_id,'status'=>1])->count();
        if(empty($game_count)){
            return -4;
        }
        if($this->create($data)){
                   $tid = $this->add();
                   return $tid ? $tid : -2; //0-未知错误，大于0-添加成功
        } else {
                   return $this->getError(); //错误详情见自动验证注释
        }
    }
}
